package com.atguigu.gmallpublisher1.service.impl;

import com.atguigu.gmallpublisher1.mapper.ProductStatsMapper;
import com.atguigu.gmallpublisher1.service.ProductStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductStatsServiceImpl implements ProductStatsService {

    @Autowired
    private ProductStatsMapper productStatsMapper;

    @Override
    public BigDecimal getGmv(int date) {
        return productStatsMapper.selectGmv(date);
    }

    /*List[
      Map[(tm_id->8),(tm_name->索芙特),(order_amount->11642.90)],
      Map[(tm_id->5),(tm_name->小米),(order_amount->125560.00)],
      ...
     ]*/
    //==>
    /*
     * Map[(索芙特->11642.90),(小米->125560.00),...]
     */
    @Override
    public Map getGmvByTrademark(int date, int limit) {

        //创建Map用于存放最终结果
        HashMap<String, BigDecimal> resultMap = new HashMap<>();

        //查询ClickHouse获取品牌GMV值
        List<Map> maps = productStatsMapper.selectGmvByTrademark(date, limit);

        //遍历maps,将数据取出并封装进resultMap
        for (Map map : maps) {
            resultMap.put((String) map.get("tm_name"), (BigDecimal) map.get("order_amount"));
        }

        //返回结果
        return resultMap;
    }
}
